package com.animation_study.custom;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by css on 2017/12/11.
 */

public class BallView extends View {
    public BallView(Context context) {
        this(context, null);
    }

    public BallView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BallView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private Path mMovePath;
    private PointF mStartPoint;
    private PointF mEndPoint;
    private PointF mFlagPoint;


    private Paint mPaint;
    private Paint mPathPaint;
    float x = 100, y = 200;
    float ra = 30;

    private void initView() {
        mPaint = new Paint();
        mPaint.setColor(Color.RED);
        mPaint.setAntiAlias(true);

        mPathPaint = new Paint();
        mPathPaint.setColor(Color.RED);
        mPathPaint.setAntiAlias(true);
        mPathPaint.setStyle(Paint.Style.STROKE);

        mStartPoint = new PointF(x, y);
        mFlagPoint = new PointF(x + 600, y - 400);
        mEndPoint = new PointF(900, 1800);
        mMovePath = new Path();
        mMovePath.moveTo(x, y);
        mMovePath.quadTo(mFlagPoint.x, mFlagPoint.y, mEndPoint.x, mEndPoint.y);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawCircle(x, y, ra, mPaint);

        canvas.drawPath(mMovePath, mPathPaint);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        if (action == MotionEvent.ACTION_DOWN) {
            ValueAnimator valueAnimator = ValueAnimator.ofObject(new MoveEvalutor(mFlagPoint), mStartPoint, mEndPoint);
            valueAnimator.setDuration(1000);
            valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator valueAnimator) {
                    PointF pointF = (PointF) valueAnimator.getAnimatedValue();
                    x = pointF.x;
                    y = pointF.y;
                    invalidate();
                    Log.d("adsf", x + "");
                }
            });
            valueAnimator.start();

        }
        return super.onTouchEvent(event);

    }
}
